package edu.hsbremen.android;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyTerminKalenderActivity extends Activity {
	
	private MyBroadcastReciever broadcastReciever; 
	
	// Initiate Elements
	EditText hugeText;
	Button btn1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        //What xml layout will be displayed
        setContentView(R.layout.main);
        
        //register all visible elements
        hugeText = (EditText) findViewById(R.id.editText1);
        btn1 = (Button) findViewById(R.id.button1);        
        
        //register a broadcastReceiver
        IntentFilter intentFilter = new IntentFilter("edu.hsbremen.android.MyTerminKalender.intent.action.BROADCAST");
        broadcastReciever = new MyBroadcastReciever();
        getApplicationContext().registerReceiver(broadcastReciever, intentFilter);
                
        // Set Listener for the Button
        btn1.setOnClickListener(new View.OnClickListener() {
			
        	//react on clicks 
			public void onClick(View v) {
				//Create a new Intent and tie it to the two activities
				Intent intend = new Intent(MyTerminKalenderActivity.this, NewAppointmentActivity.class);	
				//Start the activity with the intend and listen for a result
				startActivity(intend);
			}
		});
    }
    
    /**
     * When receiving the BroadcastReceiver creating a bundle
     * and writing its content into the big text field.
     */
    private class MyBroadcastReciever extends BroadcastReceiver{
		public void onReceive(Context ctxt, Intent intent) {
			
			Bundle bdl = intent.getExtras();
			String appointmentText = 	bdl.getString("what") + "\n" + 
										bdl.getString("when") + "\n" + 
										bdl.getString("time") + "\n" + 
										bdl.getString("where") + "\n" +
										"Calendar: " + bdl.getBoolean("saveInCalendar") + "\n" + 
										"Contact: " + bdl.getBoolean("inviteContact") + "\n" + 
										"---\n";
			 
			hugeText.setText(hugeText.getText() + appointmentText);
				
		}
    }
        
    protected void onDestroy(){
    	getApplicationContext().unregisterReceiver(broadcastReciever);
    	super.onDestroy();
    }
}